//
//  MainViewController.m
//  iPhoneKML
//
//  Created by Benjamin Blundell on 10/11/2009.
//  Copyright 2009 Section9. All rights reserved.
//

#import "MyMapViewController.h"


@implementation MyMapViewController

@synthesize tableMapData;
@synthesize delegate;

// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
    [super viewDidLoad];
	
#ifdef DEBUG
	NSLog(@"MyMapView Controller Memory Location: %@",self);
	NSLog(@"MyMapView Controller Data Location: %@",self.tableMapData);
#endif
	
}


// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}


- (void)didReceiveMemoryWarning {
	// Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
	
	// Release any cached tableData, images, etc that aren't in use.
}

- (void)viewDidUnload {
	// Release any retained subviews of the main view.
	// e.g. self.myOutlet = nil;
}


- (void)dealloc {
    [super dealloc];
	[self.tableMapData release];

}

#pragma mark -
#pragma mark Table Delegate Methods

- (NSInteger)tableView:(UITableView *)table numberOfRowsInSection:(NSInteger)section {
	
	NSLog(@"%@",table);
	if (self.tableMapData){
		//return [[self.tableMapData allKeys] count]; 
		return [self.tableMapData count];
	}
	return 0;
}

- (UITableViewCell *)tableView:(UITableView *)tv cellForRowAtIndexPath:(NSIndexPath *)indexPath {
	
	static NSString *MyMapTableIdentifier = @"MyMapTableIdentifier";
	UITableViewCell *cell = [tv dequeueReusableCellWithIdentifier: MyMapTableIdentifier];

	if (cell == nil) { 
		cell = [[[UITableViewCell alloc]
				 initWithStyle:UITableViewCellStyleDefault reuseIdentifier:MyMapTableIdentifier] autorelease];
	}
	
	//cell.textLabel.text = [[self.tableMapData allKeys] objectAtIndex:indexPath.row];
	cell.textLabel.text = [self.tableMapData objectAtIndex:indexPath.row];

	return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

	// Annoyingly, we cant use an integer position as it's not guaranteed. The label is the key which isn't so good
	UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
	NSString *key = cell.textLabel.text;
	
#ifdef DEBUG
	NSLog(@"Key Selected: %@", key);
#endif
	
	if (self.delegate){
		[self.delegate myMapViewController:self didSelectMap:key];
	}
}


/*- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {

}*/




@end
